package com.xiyu.service.repository.restaurant.recipe;

import com.xiyu.service.model.restaurant.recipe.RestaurantCombo;
import com.xiyu.service.model.restaurant.recipe.RestaurantComboTable;
import com.xiyu.service.vo.restaurant.recipe.RecipeComboQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

public interface RestaurantComboRepository extends JRepository<RestaurantCombo, Long> {
    RestaurantComboTable restaurantComboTable = RestaurantComboTable.$;

    Optional<RestaurantCombo> findFirstByComboName(String comboName);

    Optional<RestaurantCombo> findFirstByComboNameAndIdNot(String comboName, Long id);

    default List<RestaurantCombo> listQueryCombo(){
        return sql().createQuery(restaurantComboTable)
                .orderBy(restaurantComboTable.id())
                .select(restaurantComboTable).execute();
    }

    default Page<RestaurantCombo> pageQuery(RecipeComboQueryInput inputVO){
        return sql().createQuery(restaurantComboTable)
                .whereIf(StringUtils.hasText(inputVO.getComboName()), () -> restaurantComboTable.comboName().like(inputVO.getComboName()))
                .whereIf(StringUtils.hasText(inputVO.getRemark()), () -> restaurantComboTable.remark().like(inputVO.getRemark()))
                .orderBy(restaurantComboTable.id().desc())
                .select(restaurantComboTable)
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }


}